/*
 *	pulse.c
 *
 *	Unix Version 2.4 by Steve Sampson, Public Domain, September 1988
 *
 *	This program is used to generate time domain pulse data	for fft.c.
 */

#include <stdio.h>
#include <malloc.h>

#define	Chunk	(Samples * sizeof(double))

static double	Sample, Pw, Time, *Real, *Imag;
static int	Samples;
static void	err_report();
static FILE	*Fp;


main(argc, argv)
int	argc;
char	**argv;
{
	register int	loop;

	if (argc != 3)
		err_report(0);

	Samples = abs(atoi(*++argv));

	if ((Real = (double *)malloc(Chunk)) == NULL)
		err_report(1);

	if ((Imag = (double *)malloc(Chunk)) == NULL)
		err_report(2);

	printf("Input The Pulse Width (Seconds) ? ");
	scanf("%lf", &Pw);
	printf("Input The Sampling Time (Seconds) ? ");
	scanf("%lf", &Sample);

	Time = 0.0;
	for (loop = 0; loop < Samples; loop++)  {
		if (Time < Pw)
			Real[loop] = 1.0;
		else
			Real[loop] = 0.0;

		Imag[loop] = 0.0;
		Time += Sample;
	}

	if ((Fp = fopen(*++argv, "w")) == NULL)
		err_report(3);

	fwrite(Real, sizeof(double), Samples, Fp);
	fwrite(Imag, sizeof(double), Samples, Fp);
	fclose(Fp);

	putchar('\n');

	free((char *)Real);
	free((char *)Imag);

	exit(0);
}


static void err_report(n)
int	n;
{
	switch (n)  {
	case 0:
		fprintf(stderr,"Usage: pulse samples output_file\n");
		fprintf(stderr,"Where samples is a power of two\n");
		break;
	case 1:
		fprintf(stderr,"pulse: Out of memory getting real space\n");
		break;
	case 2:
		fprintf(stderr,"pulse: Out of memory getting imag space\n");
		free((char *)Real);
		break;
	case 3:
		fprintf(stderr,"pulse: Unable to create write file\n");
	}

	exit(1);
}

/* EOF */
